package com.tonicgb.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tonicgb.mapper.UserTokenMapper;
import com.tonicgb.model.UserToken;
import com.tonicgb.shared.core.crud.service.CrudService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhaiyujin
 * @date 2021-10-05 09:44:36
 * @说明 用户token信息管理
 */
@Service
public class UserTokenService extends CrudService<UserTokenMapper, UserToken> {

    /**
     * @author zhaiyujin
     * @date 2021-10-05 10:05:42
     * @说明 检测token是否存在,wechat token输入是openid
     */
    @Autowired
    private UserTokenMapper userTokenMapper;
    public UserToken checkToken(String token, int type) {
        LambdaQueryWrapper<UserToken> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(UserToken::getType, type).eq(UserToken::getToken, token);
        return userTokenMapper.selectOne(lambdaQueryWrapper);
    }


    /**
     * 绑定token关系
     * @param token String token
     * @param type int 类型
     * @since 2020-05-25
     */
    public void bind(String token, int type, Integer userId) {
        UserToken userToken = new UserToken();
        userToken.setToken(token);
        userToken.setType(type);
        userToken.setUid(userId);
        save(userToken);
    }

}
